#!/bin/bash

sed -i 's/nameserver/#nameserver/g' /etc/resolv.conf

mysqltarname="mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz"
mysqlpkgname="mysql-8.0.23-linux-glibc2.12-x86_64"
currentpath=$(cd `dirname $0`; pwd)
mysqlsoftpath="/VI"

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

echo "========================================================================="
echo "A tool to auto-compile & install MySQL 8.0.x on Redhat/CentOS/EulerOS Linux"
echo "========================================================================="

# check installed mysql or not
for i in `rpm -qa | grep "mysql"`
do 
rpm -e --allmatches $i --nodeps
done

# Remove pre-installed on OS MariaDB if exists
for i in $(rpm -qa | grep mariadb | grep -v grep)
do
  echo "Deleting rpm --> "$i
  rpm -e --nodeps $i
done

function InstallMySQL8()
{
echo "============================Install MySQL 8.0.x=================================="


#Backup old my.cnf
if [ -e /etc/my.cnf ]; then
    mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
 
echo "============================MySQL 8.0.x installing…………========================="
#mysql directory configuration
echo "tar mysql....."
echo "tar Jxvf ${currentpath}/database/mysql/${mysqltarname}"
tar Jxvf ${currentpath}/database/mysql/${mysqltarname}
echo "move dir to ${mysqlsoftpath}/mysql..."
test -d ${mysqlsoftpath} || mkdir -p ${mysqlsoftpath}
mv ${currentpath}/${mysqlpkgname} ${mysqlsoftpath}/mysql

#edit /etc/my.cnf
echo "update my.cnf"
cat >>/etc/my.cnf<<EOF
[mysqld]
datadir=${mysqlsoftpath}/mysql/data
socket=/tmp/mysql.sock
log-error=mysqld.log
pid-file=/tmp/mysqld.pid
default-authentication-plugin = mysql_native_password
lower-case-table-names=1

# 最大连接数
max_connections=5000
# 每用户最大连接数
max_user_connections=1000
# 200秒后将关闭空闲(IDLE)连接
wait_timeout=200

max_allowed_packet = 100M

#开启慢查询日志
#slow-query-log=1  
#slow-query-log-file=${mysqlsoftpath}/mysql/data/slow.log
#long_query_time=10

disable_log_bin

binlog-format=STATEMENT

innodb_buffer_pool_instances=8
#innodb_flush_method=O_DIRECT
innodb_buffer_pool_size = 3072M
innodb_log_file_size = 512M
innodb_log_buffer_size = 16M
innodb_autoextend_increment=128M
innodb_flush_log_at_trx_commit=2 
sync_binlog=10000

bulk_insert_buffer_size=100M
internal_tmp_mem_storage_engine=MEMORY
max_heap_table_size=2048M
tmp_table_size=2048M

sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
EOF

echo "add mysqluser and other dir"

groupadd mysql
useradd -g mysql mysql
test -d ${mysqlsoftpath}/mysql/data || mkdir -p ${mysqlsoftpath}/mysql/data
chmod -R 775 ${mysqlsoftpath}/mysql
chown -R mysql:mysql ${mysqlsoftpath}/mysql
  
echo "init mysql......"

${mysqlsoftpath}/mysql/bin/mysqld --user=mysql --basedir=${mysqlsoftpath}/mysql --datadir=${mysqlsoftpath}/mysql/data --initialize-insecure

cp ${mysqlsoftpath}/mysql/support-files/mysql.server /etc/init.d/mysql

echo "add mysql to auto start"
chkconfig --add mysql
 
# 启动
sed -i "s@/usr/local@/VI@g" /etc/init.d/mysql
service mysql start

echo "add env mysql path....."
cat >>/etc/profile.d/mysql.sh<<EOF
export PATH=${mysqlsoftpath}/mysql/bin:\$PATH
export LD_LIBRARY_PATH=${mysqlsoftpath}/mysql/lib:\$LD_LIBRARY_PATH
EOF

source /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
echo $PATH

#cat >>/tmp/mysql_sec_script<<EOF
#use mysql;
#ALTER USER 'root'@'localhost' IDENTIFIED BY 'VideoInsight_123';
#create user 'root'@'%' identified by 'VideoInsight_123';
#create user 'videoinsight'@'%' identified by 'VideoInsight_123';
#create user 'videoinsightcollecter'@'%' identified by 'VideoInsight_123';
#GRANT all ON *.* TO 'root'@'%';
#GRANT all ON *.* TO 'videoinsight'@'%';
#GRANT all ON *.* TO 'videoinsightcollecter'@'%';
#FLUSH PRIVILEGES;
#EOF
#echo "看见password请输入enter键......"
#${mysqlsoftpath}/mysql/bin/mysql -uroot -p -hlocalhost < /tmp/mysql_sec_script

/usr/bin/expect << EOF
spawn ${mysqlsoftpath}/mysql/bin/mysql -uroot -p -e "use mysql;ALTER USER 'root'@'localhost' IDENTIFIED BY 'VideoInsight_123';create user 'root'@'%' identified by 'VideoInsight_123';create user 'videoinsight'@'%' identified by 'VideoInsight_123';create user 'videoinsightcollecter'@'%' identified by 'VideoInsight_123';GRANT all ON *.* TO 'root'@'%';GRANT all ON *.* TO 'videoinsight'@'%';GRANT all ON *.* TO 'videoinsightcollecter'@'%';FLUSH PRIVILEGES;"
expect "Enter password:"
send "\r"
expect eof
EOF

service mysql restart
mysqld --version

echo "============================MySQL 8.0.12 install completed========================="
}

function CheckInstall()
{

echo "===================================== Check install ==================================="
#clear
echo "Checking..."
 
if [ -s ${mysqlsoftpath}/mysql/bin/mysql ] && [ -s ${mysqlsoftpath}/mysql/bin/mysqld_safe ] && [ -s /etc/my.cnf ]; then
  echo "MySQL: OK"
  num=`netstat -tunlp|grep -w 3306| grep -w LISTEN | wc -l`
  if [ $num -eq 1 ];then
    echo "mysql start success"
  else
    echo "mysql start faild..."
    exit 1
  fi
else
  echo "Error: ${mysqlsoftpath}/mysql not found!!!MySQL install failed."
  exit 1
fi

}

function MysqlInfo(){
  echo -e "\033[32m =========================================================================
  =========Congratulations,Mysql install success!!! Information.....=======
 
        basedir: ${mysqlsoftpath}/mysql
        datadir: ${mysqlsoftpath}/mysql/data
        port: 3306
        user: root videoinsight videoinsightcollecter
        password: VideoInsight_123
        connect: mysql -uroot -pVideoInsight_123
  =========================================================================\033[0m"
}

InstallMySQL8
CheckInstall
MysqlInfo

chage -E -1 mysql
chage -M 99999 mysql

sh ${currentpath}/mysql8_init_database.sh
